
package com.fas.config;

import java.io.File;

import cn.dreampie.quartz.QuartzPlugin;

import com.fas.core.interceptor.ExceptionInterceptor;
import com.fas.core.plugin.jfinal.route.AutoBindRoutes;
import com.fas.core.plugin.shiro.core.ShiroInterceptor;
import com.fas.core.plugin.shiro.core.ShiroPlugin;
import com.fas.hehe.config.model.Dictionary;
import com.fas.hehe.config.model.DictionaryData;
import com.fas.hehe.system.model.LoginLog;
import com.fas.hehe.system.model.OperateLog;
import com.fas.hehe.system.model.OperateLogDetail;
import com.fas.hehe.system.model.Organization;
import com.fas.hehe.system.model.Resource;
import com.fas.hehe.system.model.Role;
import com.fas.hehe.system.model.RoleOfResource;
import com.fas.hehe.system.model.User;
import com.fas.hehe.system.model.UserOfRole;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
/**
 * 
 * Jfinal配置类型
 *
 * @Copyright: Copyright (c) 2016 FAS 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午2:44:18
 * @version V1.0
 */
public class AppServer extends JFinalConfig {
	public static Log log = Log.getLog(AppServer.class);
	/**
	 * 供Shiro插件使用。
	 */
	Routes routes;

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		me.setDevMode(AppConfig.me().isJFDevMode());
		me.setViewType(AppConfig.me().getJFViewType());
		me.setBaseViewPath(AppConfig.me().getJFBaseViewPath());
		me.setError401View(AppConfig.me().getJF401View());
		me.setError403View(AppConfig.me().getJF403View());
		me.setError404View(AppConfig.me().getJF404View());
		me.setError500View(AppConfig.me().getJF500View());
		me.setEncoding(AppConfig.me().getJFEncoding());
		me.setUrlParaSeparator(AppConfig.me().getJFSeparator());
		me.setBaseUploadPath(AppConfig.me().getJFUploadedFileSaveDirectory());
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		this.routes = me;
		// 自动绑定
		AutoBindRoutes abr = new AutoBindRoutes();
		abr.autoScan(true);
		me.add(abr);
		//me.add("/login", LoginController.class);
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins plugins) {
		/******************************** start c3p0连接池 start ***************************************/
		C3p0Plugin c3p0Plugin = new C3p0Plugin(AppConfig.me().getJdbcUrl(),
				AppConfig.me().getJdbcUsername(), AppConfig.me()
						.getJdbcPassword(), AppConfig.me()
						.getJdbcDriverClassName(), AppConfig.me()
						.getJdbcMaxPoolSize(), AppConfig.me()
						.getJdbcMinPoolSize(), AppConfig.me()
						.getJdbcInitialPoolSize(), AppConfig.me()
						.getJdbcMaxIdleTime(), AppConfig.me()
						.getJdbcAcquireIncrement());
		plugins.add(c3p0Plugin);

		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		plugins.add(arp);
		/*// 自动扫描插件
		TableBindPlugin tableBindDefault = new TableBindPlugin(dsMysql,
				SimpleNameStyles.LOWER);
		tableBindDefault
				.setContainerFactory(new CaseInsensitiveContainerFactory(true)); // 忽略字段大小写
		// 设置数据库方言
		try {
			tableBindDefault.setDialect((Dialect) Class.forName(
					AppConfig.me().getJdbcDialect()).newInstance());
		} catch (Exception e) {
			String errmsg = "设置数据库【DS-MYSQL】方言失败！！！";
			log.error(errmsg, e);
			throw new RuntimeException(errmsg, e);
		}*/
		arp.setDevMode(AppConfig.me().isJFDevMode());
		arp.setShowSql(AppConfig.me().isJdbcShowsql());
		
		 _MappingKit.mapping(arp);
		plugins.add(arp);

		// 缓存插件
		plugins.add(new EhCachePlugin());
		// sqlxml插件
		//plugins.add(new SqlInXmlPlugin());
		// Redis插件
		RedisPlugin redis = new RedisPlugin(AppConfig.me().getRedisCacheName(),
				AppConfig.me().getRedisHost(), AppConfig.me().getRedisPort(),
				AppConfig.me().getRedisTimeout(), AppConfig.me()
						.getRedisPassword());
		plugins.add(redis);

		// 调度插件
		QuartzPlugin quartzPlugin = new QuartzPlugin("quartzjob.properties");
		plugins.add(quartzPlugin);

		// 加载Shiro插件
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
		shiroPlugin.setLoginUrl("/page/login.jsp");
		shiroPlugin.setSuccessUrl("/");
		shiroPlugin.setUnauthorizedUrl("/page/public/error/401.jsp");
		plugins.add(shiroPlugin);
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new ShiroInterceptor());
		me.add(new ExceptionInterceptor());
		// me.add(new SessionInViewInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
	}

	@Override
	public void afterJFinalStart() {
		log.info("[JFinalConfig 启动成功]");
		super.afterJFinalStart();
	}

	@Override
	public void beforeJFinalStop() {
		log.info("JFinalConfig 准备停止>>>");
		super.beforeJFinalStop();
	}
	public static void main(String[] args) {
		 String className = "com\\fas\\config\\AppConfig.class".replaceAll("\\"+File.separator, ".");
		 System.out.println(className);
	}
}
